<?php
/**
 * Created by PhpStorm.
 * User: changyifan
 * Date: 2018/2/26
 * Time: 下午4:41
 */

namespace Home\Model;
use Think\Model\BaseModel;

class TeachingclassModel extends BaseModel{

    protected $_link = array(
        'Teachingmethod' => array(
            'mapping_type' => self::BELONGS_TO,
            'class_name' => 'Teachingmethod',
            'foreign_key' => 'tme_id',

            'mapping_fields' => 'tme_name',
            'as_fields' => 'me_name'
        )
    );
    protected $_scope = array(
        'Teachingmethod' => array(
            'where' => array('tme_id'=> ''),

        ),
        'TcIdName' => array(
//            'where' => array('tme_id'=> ''),
            'field' => array('tc_id','tc_name'),
        )
    );
    protected $_auto = array(
        array('tc_status','md5',3,'function'),
    );


    public function getTeachingClassInfoListByAll(){
        $teachingmethod = D('Teachingmethod','Model');
        $tme_info = $teachingmethod->getTeachingMethodInfoListByAll();
        $con = array();
        foreach ($tme_info as $key => $value){
            $con[$key] = $value['id'];
        }
        $tme_id = array('in',$con);
        $result = $this -> scope('TcIdName',array('where'=>array('tme_id'=>$tme_id,'tc_status'=>'1')))->select();
        if($result){
            return $result;
        }else{
            return array();
        }
    }




    public function getTeachingClassInfoListWithTeahcerMethodName($tme_id,$tc_status){

        if ($tme_id == 0){

            $teachingmethod = D('Teachingmethod','Model');
            $tme_info = $teachingmethod->getTeachingMethodInfoListByAll();
            $con = array();
            foreach ($tme_info as $key => $value){
                $con[$key] = $value['id'];
            }
            $tme_id = array('in',$con);
//            var_dump($tme_id);
        }
        if ($tc_status == 0){
            $tc_status =  array('like','%%');
        }
        $result = $this-> scope('Teachingmethod',array('where'=>array('tme_id'=>$tme_id,'tc_status'=>$tc_status))) -> relation(true) -> select();
        if($result){
            foreach($result as $key =>  $value){
                $result[$key]['tc_status'] =  getTcStatusNameById($value['tc_status']);
            }
            return $result;
        }else{
            return array();
        }

    }

}